package com.ssoclient.config;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
/**
 * @author panyuanxin
 * @date 2021年06月21日 11:51
 */
public class MySessionContext {
  private static MySessionContext instance;
  private final HashMap<String, HttpSession> sessionMap;
  private MySessionContext() {
    sessionMap = new HashMap<>();
  }
  public static MySessionContext getInstance() {
    if (instance == null) {
      instance = new MySessionContext();
    }
    return instance;
  }
  public synchronized void addSession(HttpSession session) {
    if (session != null) {
      sessionMap.put(session.getId(), session);
    }
  }
  public synchronized void delSession(HttpSession session) {
    if (session != null) {
      sessionMap.remove(session.getId());
    }
  }
  public synchronized HttpSession getSession(String sessionId) {
    if (sessionId == null) {
      return null;
    }
    return sessionMap.get(sessionId);
  }
}
